package com.whut.utils;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author fuzhenjie
 * @Description 一个获得格式化时间字符串的类
 * @Date 22:34 19.7.26
 */
public class FormatDate {

    private static DecimalFormat decimalFormat = new DecimalFormat("000");

    private static final String[] month2Season = {"spring", "spring", "spring", "summer", "summer", "summer", "autumn", "autumn", "autumn", "winter", "winter", "winter"};
    private static Map<String,List<String>> seasonMap = new HashMap<>();

    public static String  getDateStringYMDHms(){
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(currentTime);
    }

    public static String  getDateStringYMDHms1(){
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        return formatter.format(currentTime);
    }

    public static String  getDateStringYMD(){
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(currentTime);
    }

    public static String getFormatNumber000(int num){
        return decimalFormat.format(num);
    }

    /**
     *
     * 根据输入的参数 取得时间范围
     * @author fuzhenjie
     * @date 20:04 19.7.26
     * @param type 1 月 2 季 3年 返回长度为4的list，两两构成一个范围,前两个构成较早的时间范围，后两个构成较晚（最新）的时间范围
     * @return java.util.List<java.lang.String>
     */
    public static List<String> getFormatRange(int type){
        Calendar calendar = Calendar.getInstance();
        List<String> list = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        switch (type){
            case 3:
                list.add((calendar.get(Calendar.YEAR)-1)+"-01-01");
                list.add((calendar.get(Calendar.YEAR)-1)+"-12-31");
                list.add(calendar.get(Calendar.YEAR)+"-01-01");
                list.add(calendar.get(Calendar.YEAR)+"-12-31");
                break;
            case 2:
                initialSeasonMap();
                int curMonth = calendar.get(Calendar.MONTH);
                int curYear = calendar.get(Calendar.YEAR);
                List<String> list2 = seasonMap.get(month2Season[curMonth]);
                for (int i = 0;i<2;i++) {
                    list2.set(i,curYear+list2.get(i));
                }
                calendar.add(Calendar.MONTH,-3);
                int lastMonth = calendar.get(Calendar.MONTH);
                int lastYear = calendar.get(Calendar.YEAR);
                List<String> list1 = seasonMap.get(month2Season[lastMonth]);
                for (int i = 0;i<2;i++) {
                    list1.set(i,lastYear+list1.get(i));
                }
                list.addAll(list1);
                list.addAll(list2);
                break;
            case 1:
                String[] result = new String[4];
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                result[2] = format.format(calendar.getTime());
                calendar.add(Calendar.MONTH,1);
                calendar.set(Calendar.DAY_OF_MONTH,0);
                //获取本月最后一天
                result[3] = format.format(calendar.getTime());
                calendar.add(Calendar.MONTH,-1);
                //减去一个月，获取上一个月的时间
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                result[0] = format.format(calendar.getTime());
                calendar.add(Calendar.MONTH,1);
                calendar.set(Calendar.DAY_OF_MONTH,0);
                //获取上月最后一天
                result[1] = format.format(calendar.getTime());
                list = Arrays.asList(result);
                break;
            default:
        }
        return list;
    }

    private static void initialSeasonMap(){
        List<String> list_spring = new ArrayList<>(Arrays.asList("-01-01","-03-31"));
        List<String> list_summer = new ArrayList<>(Arrays.asList("-04-01","-06-30"));
        List<String> list_autumn = new ArrayList<>(Arrays.asList("-07-01","-09-30"));
        List<String> list_winter = new ArrayList<>(Arrays.asList("-09-01","-12-31"));
        seasonMap.put("spring",list_spring);
        seasonMap.put("summer",list_summer);
        seasonMap.put("autumn",list_autumn);
        seasonMap.put("winter",list_winter);
    }
}
